#ifndef __XMLPARSER_HPP__
#define __XMLPARSER_HPP__

#include <xercesc/sax2/SAX2XMLReader.hpp>
#include <xercesc/sax2/XMLReaderFactory.hpp>
#include <xercesc/util/XMLString.hpp>

#include "SAXMoviesHandler.hpp"
#include "SAXPeopleHandler.hpp"

#define MOVIES_XML "xml/movies.xml"
#define PEOPLE_XML "xml/people.xml"

class XMLParser {
public:
  bool parse_movies(DBManager& database) {
    try {
      XMLPlatformUtils::Initialize();
    } 
    catch( const XMLException & toCatch ) {
      char* message = XMLString::transcode(toCatch.getMessage());
      std::cerr << "Error during initialization! :" << std::endl;
      std::cerr << "Exception message is:" << std::endl
		<< message << std::endl;
      XMLString::release(&message);
      return false;
    }
  
    SAX2XMLReader* parser = XMLReaderFactory::createXMLReader();
  
    SAXMoviesHandler handler(database);
    parser->setContentHandler(&handler);
    parser->setErrorHandler(&handler);
  
    try {
      parser->parse(MOVIES_XML);
    }
    catch (const XMLException& toCatch) {
      char* message = XMLString::transcode(toCatch.getMessage());
      std::cerr << "Exception message is:" << std::endl
		<< message << std::endl;
      XMLString::release(&message);
      delete parser;
      XMLPlatformUtils::Terminate();
      return false;
    }
    catch (const SAXParseException& toCatch) {
      char* message = XMLString::transcode(toCatch.getMessage());
      std::cerr << "Exception message is:" << std::endl
		<< message << std::endl;
      XMLString::release(&message);
      delete parser;
      XMLPlatformUtils::Terminate();
      return false;
    }
    catch (...) {
      std::cerr << "Unexpected Exception" << std::endl;
      delete parser;
      XMLPlatformUtils::Terminate();
      return false;
    }
  
    delete parser;
    XMLPlatformUtils::Terminate();
    return true; 
  }

  bool parse_people(DBManager& database) {
    try {
      XMLPlatformUtils::Initialize();
    } 
    catch( const XMLException & toCatch ) {
      char* message = XMLString::transcode(toCatch.getMessage());
      std::cerr << "Error during initialization! :" << std::endl;
      std::cerr << "Exception message is:" << std::endl
		<< message << std::endl;
      XMLString::release(&message);
      return false;
    }
  
    SAX2XMLReader* parser = XMLReaderFactory::createXMLReader();
  
    SAXPeopleHandler handler(database);
    parser->setContentHandler(&handler);
    parser->setErrorHandler(&handler);
  
    try {
      parser->parse(PEOPLE_XML);
    }
    catch (const XMLException& toCatch) {
      char* message = XMLString::transcode(toCatch.getMessage());
      std::cerr << "Exception message is:" << std::endl
		<< message << std::endl;
      XMLString::release(&message);
      delete parser;
      XMLPlatformUtils::Terminate();
      return false;
    }
    catch (const SAXParseException& toCatch) {
      char* message = XMLString::transcode(toCatch.getMessage());
      std::cerr << "Exception message is:" << std::endl
		<< message << std::endl;
      XMLString::release(&message);
      delete parser;
      XMLPlatformUtils::Terminate();
      return false;
    }
    catch (...) {
      std::cerr << "Unexpected Exception" << std::endl;
      delete parser;
      XMLPlatformUtils::Terminate();
      return false;
    }
  
    delete parser;
    XMLPlatformUtils::Terminate();
    return true; 
  }
};

#endif
